"use strict";
define(function(require, exports, module) {
    var _public = module.exports = {}, _private = {};

    var $ = require('jquery'),
        common = require('lib/common'),
        template = require('lib/template'),
        report = require('business/report'),
        showdown = require('lib/showdown.min'),
        cache = require('lib/cache'),
        router = require('business/router');


    var Ev = {};

    function transformXSS(text) {
        return text.replace(/\>/gim, '&gt;').replace(/\</gim, '&lt;')
    }

    _private.getRange = function(){
        var win = _private.win;
        var range = win.getSelection().getRangeAt(0),
            rangeNode = null;

        if (range.commonAncestorContainer) {
            rangeNode = range.commonAncestorContainer;
        } else {
            if (range.parentElement) rangeNode = range.parentElement();
        }
        return {
            node : rangeNode,
            range : range
        }
    }

    _private.converter = new showdown.Converter();
    _private.updateMd = function(text) {
        var html = this.converter.makeHtml(transformXSS(text));
        $('#md-editor').html((html))
    };

    _private.insert = function(text){//插入替换字符串
        var win = _private.win;
        win.focus();

        var sel = win.getSelection();
        var rng = sel.getRange().At(0);
        if(text.nodeType) rng.insertNode(text);
        else rng.insertNode(rng.createContextualFragment(text));
    }

    _private.win = document.getElementById('editor').contentWindow;
    _private.doc = _private.win.document;

    _private.initEvents = function() {
        $('.page-header').on('click', '.element', function() {
            var $this = $(this), elclass = $this.data('elclass');
            var range = _private.getRange().range;
            //range.insertNode();
            //doc.focus()
            var $container = $(range.startContainer), $cp = $container.parents('div');

            if($cp.length == 0) {
                $cp = $container.parents('body');
                $cp.html('<div>' + $cp.html() + '</div>');
                $cp = $cp.find('div').eq(0);
            }

            $cp[0].className = (elclass);

        })
        $('.page-header').on('click', '.frame', function() {
            var $this = $(this), elclass = $this.data('sid');
            $(elclass).show();
            $('#mask').show();
        })
        $('.mode').on('click', 'a', function() {
            var $this = $(this);
            $this.parents('.mode').find('a').removeClass('active');
            $('html').removeClass('normal markdown').addClass($this.addClass('active').data('mode'))
        })

        $(document).on('click', '.close', function() {
            $(this).parents('.popup').hide();
            $('#mask').hide();
        })




        $(document.body).on('click', '.info-item', function() {
            // 发起请求
        })
    }



    _public.init = function() {
        var doc = _private.doc;

        doc.designMode='On';//可编辑
        doc.contentEditable = true;
        doc.open();
        doc.writeln('<html class="article"><head><link rel="stylesheet" href="../../css/article.css"/></head><body></body></html>');
        doc.close();


        $(doc).on('keyup', function(e) {
            if(e.keyCode == 13) {
                var range = _private.getRange().range;
                //range.insertNode();
                //doc.focus()
                var $container = $(range.startContainer);
                if($container.length > 0) $container[0].className = '';
            }
            _private.updateMd(doc.body.innerText);
        });

        _private.initEvents();
    };

    Ev.save = function(ctx) {
        var url = router.articleSave;
        common.sendRequest({
            url: url,
            ctx: ctx,
            method: 'post'
        }).done(function(result) {
            if(result && result.id == 1) {

            }
        })
    };

    Ev.del = function(id) {
        var url = router.articleSave;
        common.sendRequest({
            url: url,
            method: 'get',
            data: {
                id: id
            }
        }).done(function(result) {
            if(result && result.id == 1) {

            }
        })
    };

    Ev.getList = function(folder) {
        var url = router.getUrl('articleGetList', folder);
        common.sendRequest({
            url: url,
            method: 'get',
            data: {
                id: id
            }
        }).done(function(result) {
            if(result && result.id == 1) {

            }
        })
    };

    Ev.getFolder = function() {
        var url = router.getUrl('articleGetFolder');
        common.sendRequest({
            url: url,
            method: 'get',
            data: {
                id: id
            }
        }).done(function(result) {
            if(result && result.id == 1) {
                $('#page-folder-container').html(template('tpl-folder', result.data));
            }
        })
    };

});